/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import CRUD.ThreadHome;
import Utility.ThreadPollsUtility;
import beans.UserType;
import beans.VoteBean;
import java.util.Vector;
import xmlMapping.Thread;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import xmlMapping.Member;
import xmlMapping.Threadpollschoices;
import xmlMapping.Threadpollsquestions;

/**
 *
 * @author ABDELMOHSEN
 */
public class AddVote extends org.apache.struts.action.Action {
    
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        VoteBean bean=(VoteBean)form;
        ThreadHome thHome=new ThreadHome();
        Thread th=thHome.findById(Integer.parseInt(bean.getThreadID()));
        Member m=th.getMember();
        UserType type=(UserType)request.getSession().getAttribute("type");
        if(type==null||(type.equals(UserType.mem)&&m.getMemberId()!=Integer.parseInt((String) request.getSession().getAttribute("id")))) return mapping.findForward("login");
        Threadpollsquestions q=new Threadpollsquestions(th, bean.getQuest());
        Vector<Threadpollschoices> chs=new Vector<Threadpollschoices>();
        if(bean.getChoice1()!=null&&!bean.getChoice1().equals("")) chs.add(new Threadpollschoices(q, bean.getChoice1(), 0));
        if(bean.getChoice2()!=null&&!bean.getChoice2().equals("")) chs.add(new Threadpollschoices(q, bean.getChoice2(), 0));
        if(bean.getChoice3()!=null&&!bean.getChoice3().equals("")) chs.add(new Threadpollschoices(q, bean.getChoice3(), 0));
        if(bean.getChoice4()!=null&&!bean.getChoice4().equals("")) chs.add(new Threadpollschoices(q, bean.getChoice4(), 0));
        if(bean.getChoice5()!=null&&!bean.getChoice5().equals("")) chs.add(new Threadpollschoices(q, bean.getChoice5(), 0));
        ThreadPollsUtility model=new ThreadPollsUtility();
        model.addThreadPolls(q, chs, m);
        return mapping.findForward("done");
    }
}
